#include "function.h"
#include "snake.h"
#include "food.h"
#include "wall.h"
int main(){
start:
    system("mode con cols=120 lines=43");//调整控制台大小
    wall w;
    food f;
    deque<Snake> s;//创建对象
    double speed = 100.0;//设置速度
    int score = 0;
    bool first_time = true;
    char dir;
    w.print_wall();//打印墙
    HideCursor();//设置光标属性
    while(true){
        while(first_time) {//判断游戏是不是刚开始
            dir = getch();//获取键入字符
            if(dir=='w'||dir=='s'||dir=='a'||dir=='d'){
                Snake head('@', dir);//设置蛇头字符样式，传入初始方向
                s.push_front(head);//入队
                for (int i = 0; i < 4; i++) {
                    Snake body(s[i].get_x() - 1, s[i].get_y(), 'd');
                    s.push_back(body);
                }//创建初始蛇身
                first_time = false;
            }else continue;
        }
        print_snake(s);//打印蛇
        gotoxy(0,height+2);//设置输出位置
        cout<<"your score: "<<score;//输出成绩
        Sleep(speed);//设置时间间隔
        if(s[0].get_x()==1||s[0].get_x()==width-1||s[0].get_y()==1||s[0].get_y()==height-1) {//判断是否撞墙
            color(12);
            gotoxy(width/2-9,height/2);
            cout<<"hit the wall,game over.."<<endl;
            gotoxy(width/2-5,height/2+2);
            cout<<"your score:"<<score<<endl;//游戏结束了输出成绩
            gotoxy(width/2-20,height/2+4);
            cout<<"press r to start a new game, f to close the window";//输出提示
            while(dir = getch()){  
                if(dir == 'f') return 0;
                else if(dir == 'r') goto start;
            }
        }
        if(eat_self(s)) {//判断是否碰到自身
            gotoxy(width/2-9,height/2);
            cout<<"eat yourself,game over.."<<endl;
            gotoxy(width/2-5,height/2+2);
            cout<<"your score:"<<score<<endl;//游戏结束输出成绩
            gotoxy(width/2-20,height/2+4);
            cout<<"press r to start a new game, f to close the window";//输出提示
            while(dir = getch()){  
                if(dir == 'f') return 0;
                else if(dir == 'r') goto start;
            }
        }

        if(is_eat_food(s,f.get_x(),f.get_y())){//判断是否吃到食物
            f.eat_food();
            Snake body(s.back().get_x()-1,s.back().get_y(),s.back().get_dir());
            s.push_back(body);
            score ++;//生长并增加成绩
        }
        if(kbhit()){//判断是否有输入
            dir = getch();
            if(dir == 'w'&&s[0].get_dir()!='w'&&s[0].get_dir()!='s') s[0].change_dir('w');
            if(dir == 'a'&&s[0].get_dir()!='a'&&s[0].get_dir()!='d') s[0].change_dir('a');
            if(dir == 's'&&s[0].get_dir()!='w'&&s[0].get_dir()!='s') s[0].change_dir('s');
            if(dir == 'd'&&s[0].get_dir()!='a'&&s[0].get_dir()!='d') s[0].change_dir('d');//改变方向
            if(dir == ' ') {//暂停游戏
                gotoxy(width/2,height/2);
                cout<<"game pause,press whatever to continue..."<<endl;
                getch();
                gotoxy(width/2,height/2);
                cout<<"                                          "<<endl;
            }
            if(dir == 'v'&&speed>=50.0) speed = speed_up(speed);
            if(dir == 'b'&&speed<=200.0) speed = speed_down(speed);//加速减速
        }
        if(!f.get_flag()){
            f.create_food(s);
        }//创建新食物
        clear_snake(s);
        Snake temp = s.front();
        s.front().change_body();
        temp.move();
        s.push_front(temp);
        s.pop_back();//移动蛇
    }

}
